import { defineStore } from 'pinia'
import server from '@/api/index'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'

export const usePositionStore = defineStore('position', {
  state: () => ({
    positionList: [],
    page: 1,
    pageSize: 20,
    total: 0,
    loading: false,
    openDrawer: false,
    addFlag: false,
    editFlag: false,
    editData: {},
    statusEnums: [],
    positionFlag: true,
    title:'',
  }),
  actions: {
    // 格式化日期
    formatDateRange(startDate, endDate) {
      const formattedStart = dayjs(startDate).format('YYYY/MM/DD ')
      const formattedEnd = dayjs(endDate).format('YYYY/MM/DD ')
      return `${formattedStart} 至 ${formattedEnd}`
    },
    async getMyPositionList(userID) {
      this.loading = true
      try {
        let params = {
          userId: userID,
          pageSize: this.pageSize,
          current: this.page,
        }
        const res = await server.getlistMyPostVOByPage(params)
        if (res.data.code === 0) {
          this.total = res.data.data.total
          this.positionList = res.data.data.records
          this.positionList.forEach(item => {
            if (item.startDate && item.endDate) {
              const formattedStart = dayjs(item.startDate).format('YYYY/MM/DD ')
              const formattedEnd = dayjs(item.endDate).format('YYYY/MM/DD ')
              item.duration = `${formattedStart} 至 ${formattedEnd}`
              switch (item.status) {
                case 0:
                  item.status = '已创建'
                  break
                case 1:
                  item.status = '已发布'
                  break
                case 2:
                  item.status = '已暂停'
                  break
                case 3:
                  item.status = '已关闭'
                  break
                case 4:
                  item.status = '已招满'
                  break
              }
            } else {
              item.duration = ''
            }
          })
        } else {
          message.error(`获取我的职位列表失败: ${res.data.message}`)
        }
      } catch (e) {
        message.error(`获取我的职位列表时发生错误: ${e.message}`)
      }finally {
        this.loading = false}
    },
    // 获取全部状态下列表
    async getAllPositionList() {
      try {
        this.loading = true
        let params = {
          pageSize: this.pageSize,
          current: this.page,
        }
        const res = await server.getlistPostVOByPage(params)
        if (res.data.code === 0) {
          this.total = res.data.data.total
          this.positionList = res.data.data.records

          this.positionList.forEach(item => {
            if (item.startDate && item.endDate) {
              const formattedStart = dayjs(item.startDate).format('YYYY/MM/DD ')
              const formattedEnd = dayjs(item.endDate).format('YYYY/MM/DD ')
              item.duration = `${formattedStart} 至 ${formattedEnd}`
              switch (item.status) {
                case 0:
                  item.status = '已创建'
                  break
                case 1:
                  item.status = '已发布'
                  break
                case 2:
                  item.status = '已暂停'
                  break
                case 3:
                  item.status = '已关闭'
                  break
                case 4:
                  item.status = '已招满'
                  break
              }
            } else {
              item.duration = ''
            }
          })
        } else {
          message.error(`获取所有职位列表失败: ${res.data.message}`)
        }
      } catch (e) {
        message.error(`获取所有职位列表时发生错误: ${e.message}`)
      }finally {
        this.loading = false}
    },
  },
})
